package com.example.expresslove;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LetterActivity extends Activity {
	int count = -1;
	private ImageView image;
	private TextView text_line1, text_line2, text_line3, text_line4,
			text_line5, text_line6, text_line7, text_line8, text_line9,
			text_line10, text_line11, text_line12, text_line13, text_line14,
			text_line15, text_line16, text_line17, text_line18;

	private Handler handler = null;
	Thread thread;
	Boolean flag = true;
	private long mExitTime = 0;//双击退出变量

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.letter);
		initView();
		handler = new Handler();
		thread = new Thread() {
			public void run() {
				while (flag) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					count++;
					if (count == 20) {
						flag = false;
						Intent intent = new Intent();
						intent.setClass(LetterActivity.this,
								TheEndActivity.class);
						startActivity(intent);
						finish();
						overridePendingTransition(R.anim.out_alpha,
								R.anim.enter_alpha);
					}
					if (count < 20) {
						//Log.i("SSSSSSSSSSSSSSSSSSSSSSS", "you are my 儿子");
						handler.post(runnableUi);
					}
				}
			}
		};
		thread.start();
	}

	private void initView() {
		text_line1 = (TextView) findViewById(R.id.id_line1);
		text_line2 = (TextView) findViewById(R.id.id_line2);
		text_line3 = (TextView) findViewById(R.id.id_line3);
		text_line4 = (TextView) findViewById(R.id.id_line4);
		text_line5 = (TextView) findViewById(R.id.id_line5);
		text_line6 = (TextView) findViewById(R.id.id_line6);
		text_line7 = (TextView) findViewById(R.id.id_line7);
		text_line8 = (TextView) findViewById(R.id.id_line8);
		text_line9 = (TextView) findViewById(R.id.id_line9);
		text_line10 = (TextView) findViewById(R.id.id_line10);
		text_line11 = (TextView) findViewById(R.id.id_line11);
		text_line12 = (TextView) findViewById(R.id.id_line12);
		text_line13 = (TextView) findViewById(R.id.id_line13);
		text_line14 = (TextView) findViewById(R.id.id_line14);
		text_line15 = (TextView) findViewById(R.id.id_line15);
		text_line16 = (TextView) findViewById(R.id.id_line16);
		text_line17 = (TextView) findViewById(R.id.id_line17);
		text_line18 = (TextView) findViewById(R.id.id_line18);

		Typeface face = Typeface.createFromAsset(getAssets(),
				"fonts/chinese.ttf");
		text_line1.setTypeface(face);
		text_line2.setTypeface(face);
		text_line3.setTypeface(face);
		text_line4.setTypeface(face);
		text_line5.setTypeface(face);
		text_line6.setTypeface(face);
		text_line7.setTypeface(face);
		text_line8.setTypeface(face);
		text_line9.setTypeface(face);
		text_line10.setTypeface(face);
		text_line11.setTypeface(face);
		text_line12.setTypeface(face);
		text_line13.setTypeface(face);
		text_line14.setTypeface(face);
		text_line15.setTypeface(face);
		text_line16.setTypeface(face);
		text_line17.setTypeface(face);
		text_line18.setTypeface(face);
		text_line1.setVisibility(View.INVISIBLE);
		text_line2.setVisibility(View.INVISIBLE);
		text_line3.setVisibility(View.INVISIBLE);
		text_line4.setVisibility(View.INVISIBLE);
		text_line5.setVisibility(View.INVISIBLE);
		text_line6.setVisibility(View.INVISIBLE);
		text_line7.setVisibility(View.INVISIBLE);
		text_line8.setVisibility(View.INVISIBLE);
		text_line9.setVisibility(View.INVISIBLE);
		text_line10.setVisibility(View.INVISIBLE);
		text_line11.setVisibility(View.INVISIBLE);
		text_line12.setVisibility(View.INVISIBLE);
		text_line13.setVisibility(View.INVISIBLE);
		text_line14.setVisibility(View.INVISIBLE);
		text_line15.setVisibility(View.INVISIBLE);
		text_line16.setVisibility(View.INVISIBLE);
		text_line17.setVisibility(View.INVISIBLE);
		text_line18.setVisibility(View.INVISIBLE);
	}
	Runnable runnableUi = new Runnable() {
		@Override
		public void run() {
			if (count == 1) {
				text_line1.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line1.startAnimation(alphaAnimation);
			} else if (count == 2) {
				text_line2.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line2.startAnimation(alphaAnimation);
			} else if (count == 3) {
				text_line3.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line3.startAnimation(alphaAnimation);
			} else if (count == 4) {
				text_line4.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line4.startAnimation(alphaAnimation);
			} else if (count == 5) {
				text_line5.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line5.startAnimation(alphaAnimation);
			} else if (count == 6) {
				text_line6.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line6.startAnimation(alphaAnimation);
			} else if (count == 7) {
				text_line7.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line7.startAnimation(alphaAnimation);
			} else if (count == 8) {
				text_line8.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line8.startAnimation(alphaAnimation);
			} else if (count == 9) {
				text_line9.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line9.startAnimation(alphaAnimation);
			} else if (count == 10) {
				text_line10.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line10.startAnimation(alphaAnimation);
			} else if (count == 11) {
				text_line11.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line11.startAnimation(alphaAnimation);
			} else if (count == 12) {
				text_line12.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line12.startAnimation(alphaAnimation);
			} else if (count == 13) {
				text_line13.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line13.startAnimation(alphaAnimation);
			} else if (count == 14) {
				text_line14.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line14.startAnimation(alphaAnimation);
			} else if (count == 15) {
				text_line15.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line15.startAnimation(alphaAnimation);
			} else if (count == 16) {
				text_line16.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line16.startAnimation(alphaAnimation);
			} else if (count == 17) {
				text_line17.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line17.startAnimation(alphaAnimation);
			} else if (count == 18) {
				text_line18.setVisibility(View.VISIBLE);
				Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
				alphaAnimation.setDuration(3000);
				text_line18.startAnimation(alphaAnimation);

			}

		}

	};


	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			//这里直接不允许返回
/*			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				Toast.makeText(this, "宝贝再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {
				finish();
				MainActivity.MP3.stop();
				System.exit(0);//及时退出防止线程混乱问题
			}*/
			Toast.makeText(this,"宝贝耐心看完呀!",Toast.LENGTH_SHORT).show();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

}
